using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.Diagnostics;

using phorm.database.core;
using phorm.model.fields;

namespace phorm.database.core
{
    /// <summary>
    /// A column in a database object.
    /// </summary>
    public class DatabaseColumn
    {
        /// <summary>
        /// The length of the column.
        /// </summary>
        public string length;
        /// <summary>
        /// The name of the column.
        /// </summary>
        public string column;
        /// <summary>
        /// The type of the column.
        /// </summary>
        public string field_type;
        /// <summary>
        /// Whether the column has changed since instantiation or save.
        /// </summary>
        public bool hasChanged;

        /// <summary>
        /// The value of this column.
        /// </summary>
        protected object _value;
        /// <summary>
        /// The model object to which this column belongs.
        /// </summary>
        protected DatabaseRecord _model;

        /// <summary>
        /// Accessor method for protected variable _model.
        /// </summary>
        public DatabaseRecord model
        {
            get
            {
                return _model;
            }
        }

        /// <summary>
        /// Add the column to the model if it has not been added already.
        /// </summary>
        protected void populateColumn()
        {
            Type t = this._model.GetType();
            foreach (FieldInfo f in t.GetFields())
            {
                if (f.GetValue(this._model) == null)
                {
                    this._model.addColumn(f.Name, this);
                    break;
                }
            }
        }

        /// <summary>
        /// Accessor methods for private variable _value. Also sets hasChanged to true to know whether or not field has been changed.
        /// </summary>
        public object value
        {
            get
            {
                return this._value;
            }

            set
            {
                this.hasChanged = true;
                this._value = value;
            }
        }
    }
}
